package com.tdx.blog_nowcoder.utils.event;

import com.alibaba.fastjson.JSONObject;
import com.tdx.blog_nowcoder.entity.DiscussPort;
import com.tdx.blog_nowcoder.entity.Message;
import com.tdx.blog_nowcoder.entity.MyEvent;
import com.tdx.blog_nowcoder.service.DiscussPortService;
import com.tdx.blog_nowcoder.service.ElasticsearchService;
import com.tdx.blog_nowcoder.service.MessageService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.tdx.blog_nowcoder.constant.KafkaTopicConstant.*;

/**
* @description: TODO
* @author 何可人
* @date 2022/7/6 10:41
* @version 1.0
*/
@Component
public class EventConsumer {
    private static final Logger logger = LoggerFactory.getLogger(EventConsumer.class);

    @Autowired
    private MessageService messageService;

    @Autowired
    private DiscussPortService discussPortService;

    @Autowired
    private ElasticsearchService elasticsearchService;

    @KafkaListener(topics = {TOPIC_COMMENT,TOPIC_FOLLOW,TOPIC_LIKE})
    public void handleCommentMessage(ConsumerRecord record){
        if (record == null || record.value() == null) {
            logger.error("消息的内容为空!");
            return;
        }
        System.out.println(record.value().toString());
        MyEvent event = JSONObject.parseObject(record.value().toString(), MyEvent.class) ;
        if (event == null) {
            logger.error("消息格式错误!");
            return;
        }
        //设置消息
        Message message = new Message();
        message.setFromId(SYSTEM_ID);
        message.setToId(event.getEntityUserId());
        message.setConversationId(event.getTopic());
        message.setCreateTime(new Date());

        // {"entityType":1,"entityId":234,"postId":234,"userId":114}
        Map<String,Object> content = new HashMap<>();
        content.put("entityType",event.getEntityType());
        content.put("entityId",event.getEntityId());
        content.put("userId",event.getUserId());

        Map<String,Object> data = event.getData();
        if(data!=null&&!data.isEmpty()){
            for(Map.Entry<String,Object> entry:data.entrySet()){
                content.put(entry.getKey(),entry.getValue());
            }
        }

        message.setContent(JSONObject.toJSONString(content));
        messageService.addMessage(message);

    }

    // 消费发帖事件
    @KafkaListener(topics = {TOPIC_PUBLISH})
    public void handlePublishMessage(ConsumerRecord record) {
        if (record == null || record.value() == null) {
            logger.error("消息的内容为空!");
            return;
        }

        MyEvent event = JSONObject.parseObject(record.value().toString(), MyEvent.class);
        if (event == null) {
            logger.error("消息格式错误!");
            return;
        }

        DiscussPort post = discussPortService.getDiscussPostsById(event.getEntityId());
        elasticsearchService.saveDiscussPost(post);
    }


    // 消费发帖事件
    @KafkaListener(topics = {TOPIC_DELETE})
    public void handleDeleteMessage(ConsumerRecord record) {
        if (record == null || record.value() == null) {
            logger.error("消息的内容为空!");
            return;
        }

        MyEvent event = JSONObject.parseObject(record.value().toString(), MyEvent.class);
        if (event == null) {
            logger.error("消息格式错误!");
            return;
        }

        elasticsearchService.deleteDiscussPost(event.getEntityId());
    }


}
